package com.djq.hhc.interceptor;

import com.djq.hhc.dto.UserInfoDto;
import com.djq.hhc.holder.UserHolder;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: dadd7
 * @Date: 2023/04/09/下午 11:33
 * @Description:
 */
public class AdminInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        UserInfoDto user = UserHolder.getUser();
        if ( user == null) {
            response.setStatus(401);
            return false;
        }
        if (user.getLimits() != 1) {
            response.setStatus(402);//不是管理员
            return false;
        }
        return true;
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        //移除用户
        UserHolder.removeUser();
    }
}
